import javax.swing.*;
import java.awt.*;
/**
 * GUI for the Maze
 * 
 * @author Jofry HS
 * @version 16-07-2010
 */
public class MazeGUI extends JFrame
{
    // GUI needs access to the maze to trigger event
    private Maze maze;
    
    // Label of the status
    JLabel statusLabel;
    
    // ImagePanel is contained within
    private ImagePanel imgPanel;

    /**
     * Constructor for objects of class MazeGUI
     */
    public MazeGUI(Maze m)
    {
        // Give it name
        super("Maze Robot - Swinburne University of Technology Sarawak");
        this.setSize(850,650);
        
        maze = m;
        imgPanel = new ImagePanel(m);
        
        // Set content panel to BorderLayout
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
                
        // Directional button panel
        JPanel directionPanel = new JPanel();
        directionPanel.setLayout(new GridLayout(3,3));
        directionPanel.setPreferredSize(new Dimension(200,200));
        //directionPanel.setMaximumSize(new Dimension(100,100));
        //directionPanel.setBounds(new Rectangle(0, 0, 100, 100));
        
        JButton [] navigationBtn = {  new JButton("/\\"), new JButton("<"), new JButton(">"),
                                    new JButton("\\/"), new JButton("Stay") };
                
        JPanel [] navigationBtnHolder = new JPanel[navigationBtn.length];
        for (int i = 0; i < navigationBtn.length; i++)
        {
            navigationBtn[i].addActionListener(m);
            navigationBtnHolder[i] = new JPanel();
            navigationBtnHolder[i].add(navigationBtn[i]);
            //directionPanel.add
        }
        
        directionPanel.add(new JPanel());
        directionPanel.add(navigationBtnHolder[0]);
        directionPanel.add(new JPanel());
        directionPanel.add(navigationBtnHolder[1]);
        directionPanel.add(navigationBtnHolder[4]);
        directionPanel.add(navigationBtnHolder[2]);
        directionPanel.add(new JPanel());
        directionPanel.add(navigationBtnHolder[3]);
        directionPanel.add(new JPanel());
        /*
        directionPanel.add(navigationBtnHolder[0], BorderLayout.NORTH);
        directionPanel.add(navigationBtnHolder[1], BorderLayout.WEST);
        directionPanel.add(navigationBtnHolder[2], BorderLayout.EAST);
        directionPanel.add(navigationBtnHolder[3], BorderLayout.SOUTH);
        directionPanel.add(navigationBtnHolder[4], BorderLayout.CENTER);
        */
        JPanel directionPanelHolder = new JPanel();
        directionPanelHolder.setPreferredSize(new Dimension(200,200));        
        directionPanelHolder.add(directionPanel);
        // ----- End of Directional button panel
        
        // Reset and Undo button panel
        JButton resetBtn = new JButton("Reset");
        JButton undoBtn = new JButton("Undo");
        resetBtn.addActionListener(m);
        undoBtn.addActionListener(m);

        JPanel resetAndUndoBtnHolder = new JPanel();
        resetAndUndoBtnHolder.add(resetBtn);
        resetAndUndoBtnHolder.add(undoBtn);
        // ----- End of Reset and Undo button panel
        
        // Game status panel
        statusLabel = new JLabel();
        JPanel statusPanel = new JPanel();
        statusPanel.add(statusLabel);
               
        // Main Panel for all buttons on the left
        JPanel leftPanel = new JPanel();
        
        int numRows = m.getHeight() * 30 / 100;
        if (numRows < 3) numRows = 3;
        leftPanel.setLayout(new GridLayout(numRows,1));        
        leftPanel.setPreferredSize(new Dimension(200,300));
        leftPanel.setMaximumSize(new Dimension(200,300));
        leftPanel.add(directionPanel);       
        leftPanel.add(resetAndUndoBtnHolder);
        leftPanel.add(statusPanel);
        
        // Finally add them to the content panel of the frame
        contentPane.add(leftPanel, BorderLayout.WEST);
        contentPane.add(imgPanel, BorderLayout.CENTER);
        
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.pack();
    }
    
    public void setStatus(String text)
    {
        statusLabel.setText(text);
    }
    
    public void updateGraphics()
    {
        imgPanel.repaint();
    }
}
